Impressive
Informative
Easy
9 November 2018
Impressive
Informative
Easy
ggplot2
plotly
Tidy data
## # A tibble: 6 x 6 ## country continent year lifeExp pop gdpPercap ## <fct> <fct> <int> <dbl> <int> <dbl> ## 1 Afghanistan Asia 1952 28.8 8425333 779. ## 2 Afghanistan Asia 1957 30.3 9240934 821. ## 3 Afghanistan Asia 1962 32.0 10267083 853. ## 4 Afghanistan Asia 1967 34.0 11537966 836. ## 5 Afghanistan Asia 1972 36.1 13079460 740. ## 6 Afghanistan Asia 1977 38.4 14880372 786.
Code
gapminder %>% plot_ly(x = ~gdpPercap, y = ~lifeExp, size = ~pop, color = ~continent, frame = ~year, text = ~country, hoverinfo = "text",type = 'scatter',mode = 'markers') %>% layout(xaxis = list(type = "log"))
ggplotly functionggplot object and returns a plotly objectggplot, e.g. legend positionggplotplot_ly interfaceggplotlyggplotggplotly(Plot1_gg)
ggplot syntax: ggplot(data,mapping=aes()) + geom_bar() ...
plot_ly syntax: Borrows semantics from dplyr and tidyr packages
Attributes defined via plot_ly which sets 'global' attributes that are carried onto subsequent traces
Example:
plot_ly(economics, x = ~date, color = I("black")) %>%
add_lines(y = ~uempmed) %>%
add_lines(y = ~psavert, color = I("red"))
Using the APRA dataset
type = "box"type = "scatter"plot_ly(data = data.frame(), ..., color, text, type)
~ e.g. x=~'variable name'plot_ly(data=apra, x = ~`Ten-year rate of return`,
color = ~`Fund type`, text = ~`Fund name`, type = "box")
plot_ly(data=apra, x = ~`Operating expense ratio`,
y = ~`Investment expenses ratio`, type = 'scatter',
mode = 'markers',text = ~`Fund name`, color = ~`Fund type`)
-The subplot function provides a flexible interface for merging multiple plotly objects into a single object
-A bit like facet_wrap and facet_grid from ggplot, e.g.
p1 <- apra %>%
plot_ly(x = ~`Investment expenses ratio`, y = ~`Ten-year rate of return`,
type = 'scatter', mode = 'markers',text = ~`Fund name`,
color = ~`Fund type`,size = ~`Total assets`^2,
legendgroup= ~`Fund type`,showlegend = F)
p2 <- apra %>%
plot_ly(x = ~`Operating expense ratio`, y = ~`Ten-year rate of return`,
type = 'scatter', mode = 'markers',text = ~`Fund name`,
color = ~`Fund type`,size = ~`Total assets`^2,
legendgroup= ~`Fund type`,showlegend = F)
p3 <- apra %>%
plot_ly(x = ~`Proportion of total assets in default or MySuper strategy`,
y = ~`Ten-year rate of return`, type = 'scatter',
mode = 'markers',text = ~`Fund name`, color = ~`Fund type`,
size = ~`Total assets`^2,
legendgroup= ~`Fund type`,showlegend = T)
apra.range<- apra %>% select(`Operating expense ratio`,`Ten-year rate of return`) %>% na.omit() min.y <- floor(round(min(apra.range$`Ten-year rate of return`),1)) max.y <- ceiling(round(max(apra.range$`Ten-year rate of return`),1)) subplot(p1,p2,p3,titleY = TRUE,titleX=TRUE,shareY = TRUE) %>% layout(yaxis = list(range = c(min.y,max.y)))
plot_ly(apra, x = ~`Operating expense ratio`, y = ~`Ten-year rate of return`,
type = 'scatter', mode = 'markers',text = ~`Fund name`,
size = ~`Total assets`^2,color= ~`Fund type`)
plot_ly(apra, x = ~`Operating expense ratio`,
y = ~`Ten-year rate of return`,
type = 'scatter', mode = 'markers',text = ~`Fund name`,
size = ~`Total assets`^2,
transforms = list(
list(
type = 'filter',
target = ~`Fund type`,
operation = '=',
value = ~`Fund type`[1]))) %>%
layout(
updatemenus = list(
list(
type = 'dropdown',
active = 1,
buttons = lapply(unique(apra$`Fund type`), function (x) {
list(method = "restyle",
args = list("transforms[0].value",x),
label = x)} ) )),
xaxis = list(range = c(min.x,max.x)),
yaxis = list(range = c(min.y,max.y)))
Calculate regression coefficients and standard errors
m <- lm(data = apra,`Ten-year rate of return` ~ `Operating expense ratio`+
`Investment expenses ratio` +
`Proportion of total assets in default or MySuper strategy`)
d <- broom::tidy(m) %>% arrange(desc(estimate))
Create a scatter (with the coeffients) and use error_x to get the error bars
plot_ly(d, x = ~estimate, y = ~term) %>% add_markers(error_x = ~list(value = std.error)) %>% layout(yaxis = list(title ="", autorange = "reversed"))
ribs <- lm(`Ten-year rate of return` ~ `Operating expense ratio`,
data = apra) %>% augment(.)
apra$.rownames <- rownames(apra)
apra$.se.fit <- NULL
apra$.fitted <- NULL
apra <- left_join(apra,ribs %>% select(.rownames,.se.fit,.fitted),by=".rownames")
plot_ly(apra, x = ~`Operating expense ratio`, y = ~`Ten-year rate of return`,
type = 'scatter', mode = 'markers',text = ~`Fund name`) %>%
add_ribbons(
ymin = ~.fitted - 1.96 * .se.fit,
ymax = ~.fitted + 1.96 * .se.fit,
line = list(color = 'rgba(7, 164, 181, 0.05)'),
fillcolor = 'rgba(7, 164, 181, 0.2)',
name = "Standard Error") %>%
layout(
xaxis = list(range = c(0, 1)),
yaxis = list(range = c(-5, 5)),
showlegend = FALSE)
Resources